<template>
  <n-grid cols="1 s:1 m:1 l:4 xl:4 2xl:4" responsive="screen">
    <n-grid-item>
      <n-form :label-width="80" :model="formParams" :rules="rules" ref="formRef">
        <n-form-item label="网站名称" path="name">
          <n-input v-model:value="formParams.name" placeholder="请输入网站名称" />
        </n-form-item>

        <n-form-item label="网站域名" path="notify_url">
          <n-input v-model:value="formParams.notify_url" placeholder="请输入网站域名" />
        </n-form-item>

        <n-form-item label="备案编号" path="icpCode">
          <n-input placeholder="请输入备案编号" v-model:value="formParams.icpCode" />
        </n-form-item>

        <n-form-item label="版权信息" path="banquan">
          <n-input placeholder="请输入版权信息" v-model:value="formParams.banquan" />
        </n-form-item>

        <n-form-item label="公司名称" path="company_title">
          <n-input placeholder="请输入公司名称" v-model:value="formParams.company_title" />
        </n-form-item>

        <n-form-item label="手机号授权登录" path="phone_status">
          <n-radio-group v-model:value="formParams.phone_status" name="phone_status">
            <n-space>
              <n-radio :key="0" :value="0">关闭</n-radio>
              <n-radio :key="1" :value="1">开启</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>

        <n-form-item label="支付宝免押服务" path="alipaymianyastatus">
          <n-radio-group v-model:value="formParams.alipaymianyastatus" name="alipaymianyastatus">
            <n-space>
              <n-radio :key="1" :value="1">关闭</n-radio>
              <n-radio :key="2" :value="2">开启</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>

        <n-form-item label="最小提现金额" path="minmoney">
          <n-input v-model:value="formParams.minmoney" type="minmoney" placeholder="请输入最小提现金额" />
        </n-form-item>
        
        <n-form-item label="最大提现金额" path="maxmoney">
          <n-input v-model:value="formParams.maxmoney" type="maxmoney" placeholder="请输入最大提现金额" />
        </n-form-item>

        <!-- <n-form-item label="二轮订单开票" path="invoice_status">
          <n-radio-group v-model:value="formParams.invoice_status" name="invoice_status">
            <n-space>
              <n-radio :key="0" :value="0">关闭</n-radio>
              <n-radio :key="1" :value="1">开启</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>

        <n-form-item label="四轮菜单" path="silun_status">
          <n-radio-group v-model:value="formParams.silun_status" name="silun_status">
            <n-space>
              <n-radio :key="0" :value="0">关闭</n-radio>
              <n-radio :key="1" :value="1">开启</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item> -->

        <n-form-item label="邮箱" path="email">
          <n-input v-model:value="formParams.email" placeholder="请输入邮箱" />
        </n-form-item>

        <n-form-item label="客服电话" path="x_tell">
          <n-input v-model:value="formParams.x_tell" placeholder="请输入客服电话" />
        </n-form-item>

        <n-form-item label="商务合作电话" path="mobile">
          <n-input v-model:value="formParams.mobile" placeholder="商务合作电话" />
        </n-form-item>

        <n-form-item label="退款方式" path="returntype">
          <n-radio-group v-model:value="formParams.returntype" name="statusGroup">
            <n-space>
              <n-radio :key="1" :value="1">余额</n-radio>
              <n-radio :key="2" :value="2">原路返回</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>

        <div>
          <n-space>
            <n-button type="primary" :loading="subLoading" @click="formSubmit">更新基本信息</n-button>
          </n-space>
        </div>
      </n-form>
    </n-grid-item>
  </n-grid>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { useMessage, FormRules } from 'naive-ui';
  import { webconfigEdit,webconfiginfo } from '@/api/webconfig/index';
  
  const rules: FormRules = {
    name: {
      required: true,
      message: '请输入网站名称',
      trigger: 'blur',
    },
  };

  const defaultValueRef = () => ({
      name: '',
      mobile: '',
      icpCode: '',
      address: '',
      banquan: '',
      company_title: '',
      closeText: '',
      systemOpen: 0,
      phone_status: 0,
      notify_url: '',
      minmoney: '',
      maxmoney: '',
      invoice_status: '',
      silun_status: '',
      email: '',
      x_tell: '',
      returntype: 2,
      alipaymianyastatus: 1,
  });

  const message = useMessage();
  const formRef: any = ref(null);
  const subLoading = ref(false);
  const formParams = ref(defaultValueRef());

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        webconfigEdit(formParams.value).then((_) => {
          message.success('更新成功');
        });
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  function getInfo() {
    webconfiginfo({}).then((res) => {
      const params = {
        name: res.name,
        mobile: res.mobile,
        icpCode: res.icpCode,
        address: res.address,
        banquan: res.banquan,
        company_title: res.company_title,
        closeText: res.closeText,
        systemOpen: res.systemOpen,
        phone_status: res.phone_status,
        notify_url: res.notify_url,
        minmoney: res.minmoney,
        maxmoney: res.maxmoney,
        invoice_status: res.invoice_status,
        silun_status: res.silun_status,
        email: res.email,
        x_tell: res.x_tell,
        returntype: parseInt(res.returntype),
        alipaymianyastatus: res.alipaymianyastatus,
      };
      formParams.value = Object.assign(formParams.value, params);
    });
  }

  onMounted(async () => {
    getInfo();
  });
</script>
